package com.urfresh.sp.user.schedule.configure;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DataSourceImplantConfig {

    @Value("${com.urfresh.datasource.url}")
    String urlImport;

    @Value("${com.urfresh.datasource.username}")
    String username;

    @Value("${com.urfresh.datasource.password}")
    String password;

    @Bean
    public DataSource dataSource() {
        org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl(urlImport);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setInitialSize(1);
        ds.setMaxActive(60);
        ds.setMaxIdle(0);
        ds.setMinIdle(15);
        ds.setDefaultAutoCommit(false);
        ds.setMaxWait(5000);
        ds.setRemoveAbandoned(true);
        ds.setRemoveAbandonedTimeout(60);
        ds.setTimeBetweenEvictionRunsMillis(150000);
        ds.setTestOnBorrow(true);
        ds.setTestOnReturn(false);
        ds.setTestWhileIdle(true);
        ds.setValidationQuery("select 1");
        ds.setValidationInterval(60000);
        ds.setValidationQueryTimeout(3);
        ds.setMinEvictableIdleTimeMillis(900000);

        ds.setJdbcInterceptors(
                "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
        return ds;
    }

}
